package com.gurumades.triggers.client;

import java.util.List;
import java.util.Map;

public abstract class TriggerManager {
	
	TriggerRegistry registry;

	public static class Utils{
		private static TriggerManager instance;
		
		public static TriggerManager instance(){
			if (instance == null) {
				instance = new TriggerManagerImpl();
			}
			
			return instance;
		}
	}

	public void setRegistry(TriggerRegistry registry){
		this.registry = registry;
	}

	public TriggerRegistration trigger(Trigerable eq){
		return new TriggerRegistration(eq, registry);
	}

	public void postEvent(TriggerChangeEvent triggerChange) {
		registry.reportEvent(triggerChange);
		
		Trigger source = triggerChange.getSource();
		Object value = triggerChange.getValue();
		
		Map<Trigerable, Boolean> matchings = registry.getMatchings(source, value);
		
		for (Trigerable trigerable : matchings.keySet()) {
			trigerable.trigger(matchings.get(trigerable));
			//System.err.println("Triggering "+trigerable+(""+trigerable+""));
		}
		
	}

	public void reset() {
		registry.reset();
	}
}
